#include "test_people.h"
#include "collision_manager.h"

using namespace alice;

People::People(Properties* props):Character(props) {
  animation_.SetProps(texture_id_, 1, 16, 60);

  is_jumping_ = is_grounded_ = false;
  jump_time_ = kJumpTime;
  jump_force_ = kJumpForce;
  attack_time_ = kAttackTime;
  is_attacking_ = false;
  flip_ = SDL_FLIP_NONE;

  collision_box_ = CollisionBox();
  collision_box_.SetBuffer(-50, -30, 0, 0);
}

void People::Draw() {
  animation_.Draw(transform_->X, transform_->Y, width_, height_, flip_);
}

void People::Update(double delta_time) {
  is_runnning_ = false;

  if (EventsManager::GetInstance()->GetAxisKey(HORIZONTAL) == kForward && !is_attacking_) {
    rigid_body_.ApplyForceX(kForward*kRunForce);
    flip_ = SDL_FLIP_NONE;
    is_runnning_ = true;
  }
  if (EventsManager::GetInstance()->GetAxisKey(HORIZONTAL) == kBackward && !is_attacking_) {
    rigid_body_.ApplyForceX(kBackward*kRunForce);
    flip_ = SDL_FLIP_HORIZONTAL;
    is_runnning_ = true;
  }
 
  if (EventsManager::GetInstance()->IsKeyDown(SDL_SCANCODE_J) && !is_runnning_) {
    rigid_body_.UnsetForce();
    is_attacking_ = true;
  }

  // 跳跃
  if (EventsManager::GetInstance()->IsKeyDown(SDL_SCANCODE_K) && is_grounded_) {
    is_jumping_ = true;
    is_grounded_ = false;
    rigid_body_.ApplyForceY(kUpward * kJumpForce);
  }
  if (EventsManager::GetInstance()->IsKeyDown(SDL_SCANCODE_K) && is_jumping_) {
    if (jump_time_ > 0) {
      jump_time_ -= delta_time;
      rigid_body_.ApplyForceY(kUpward * kJumpForce);
    } else {
      is_jumping_ = false;
      jump_time_ = kJumpTime;
    }
  }

  rigid_body_.Update(delta_time);
  last_safe_position_.X = transform_->X;
  transform_->X += rigid_body_.Position().X;
  collision_box_.SetRect(transform_->X, transform_->Y, 50, 50);
  if (CollisionManager::GetInstance()->MapCollision(collision_box_.GetRect())) {
    transform_->X = last_safe_position_.X;
  }

  rigid_body_.Update(delta_time);
  last_safe_position_.Y = transform_->Y;
  transform_->Y += rigid_body_.Position().Y;
  collision_box_.SetRect(transform_->X, transform_->Y, 50, 50);
  if (CollisionManager::GetInstance()->MapCollision(collision_box_.GetRect())) {
    is_grounded_ = true;
    transform_->Y = last_safe_position_.Y;
  } else {
    is_grounded_ = false;
  }
  
  // attack timer
  if (is_attacking_ && attack_time_ > 0) {
    attack_time_ -= delta_time;
  } else {
    is_attacking_ = false;
    attack_time_ = kAttackTime;
  }

  //

  origin_->X = transform_->X + width_ / 2;
  origin_->Y = transform_->Y + height_ / 2;

  AnimationState();
  animation_.Update(delta_time);
}

void People::Clean() {
  TextureManager::GetInstance()->Clean();
}

// TODO
// 1. 播放攻击动画时会出现闪帧现象
// 原因不明
// 2. 地图碰撞有大问题，需要修改。
void People::AnimationState() {
  rigid_body_.UnsetForce();
  animation_.SetProps("spr-idle", 1, 16, 60);
  if (is_runnning_) {
    animation_.SetProps("spr-walk", 1, 8, 60);
  }
  if (is_attacking_) {
    animation_.SetProps("spr-attack", 1, 30, 60);
  } 
}